package com.gitee.hongruisun.promotion.loginadapter.v2;

import com.gitee.hongruisun.promotion.loginadapter.ResultMsg;
import com.gitee.hongruisun.promotion.loginadapter.SiginService;

/**
 * 三方登录适配器
 */
public class PassportForThirdAdapter extends SiginService implements IPassportForThird{

    private ResultMsg processLogin(String key, Class<? extends LoginAdapter> clazz) {
        try{
            LoginAdapter adapter = clazz.newInstance();
            if(adapter.support(adapter)){
                return adapter.login(key,adapter);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * QQ登录
     *
     * @param id
     * @return
     */
    @Override
    public ResultMsg loginForQQ(String id) {
        return processLogin(id,LoginForQQAdapter.class);
    }



    /**
     * 微信登录
     *
     * @param id
     * @return
     */
    @Override
    public ResultMsg loginForWechat(String id) {
        return processLogin(id,LoginForWechatAdapter.class);
    }

    /**
     * 手机号登录
     *
     * @param telphone
     * @param code
     * @return
     */
    @Override
    public ResultMsg loginForTelphone(String telphone, String code) {
        return processLogin(telphone,LoginForTelphoneAdapter.class);
    }

    /**
     * token 登录
     *
     * @param id
     * @return
     */
    @Override
    public ResultMsg loginForToken(String id) {
        return processLogin(id,LoginForTokenAdapter.class);
    }

    /**
     * 注册后自动登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public ResultMsg loginForRegist(String username, String password) {
        super.regist(username,null);
        return super.login(username,null);
    }
}
